package week1;


import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class MyClassLoader extends ClassLoader {
    public static void main(String[] args) throws Exception {
        Class classHello = new MyClassLoader().loadClass("Hello");
        Object helloObj = classHello.newInstance();
        Method method = classHello.getDeclaredMethod("hello");
        method.invoke(helloObj);
    }

    @Override
    protected Class<?> findClass(String name) throws ClassNotFoundException {
        InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(name.replace(".", "/") + ".xlass");
        try {
            int length = inputStream.available();
            byte[] byteArray = new byte[length];
            inputStream.read(byteArray);
            // 转换
            for (int i = 0; i < byteArray.length; i++) {
                byteArray[i] = (byte) (255 - byteArray[i]);
            }
            // 通知底层定义这个类
            return defineClass(name, byteArray, 0, byteArray.length);
        } catch (IOException e) {
            throw new ClassNotFoundException(name, e);

        }
    }
}